﻿using CLVisionTrace;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using static LightControllerDll.CstClassLibControllerDll;
using static LightControllerDll.RsClassLibControllerDll;
namespace CLVisionLightControlTS
{
#if _WIN32
    using ControllerHandle = Int32;
    using ControllerHandleType = Int32;
#else
    using ControllerHandle = Int64;
    using ControllerHandleType = Int64;
#endif

    internal class SoftTriggerControl_CST
    {

        private static ControllerHandle controllerHandle = 0;
        public static int Rs232_or_Ethernet = Rs232Mode; //EthernetMode
        public static int connect_state = 0;

        // 静态变量保存类的实例
        private static SoftTriggerControl_CST instance = null;

        // 锁对象，用于确保线程安全
        private static readonly object padlock = new object();

        //实例化LogTrace对象（设成静态）
        private static LogTrace trace = new LogTrace();

        //准备日志保存路径
        private static MethodBase currentMethod = MethodBase.GetCurrentMethod();
        private static string namespaceName = currentMethod.DeclaringType.Namespace;
        private static string basePath = "D://OpconData//Trace";
        //记录日志全局开关
        private static bool g_LogEnable = true;
        public static bool G_LogEnable { get => g_LogEnable; set => g_LogEnable = value; }

        /// <summary>
        /// Import the configuration into the light source controller.
        /// </summary>
        /// <param name="CsvPath">the config file path</param>
        /// <param name="ErrorCode">the code for error</param>
        /// <param name="ErrorText">the message for error</param>
        /// <returns></returns>
        public static bool UpdateInfo(string CsvPath, string Ip, int ControllHandle, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            controllerHandle = ControllHandle;
            int result = 0;
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            try
            {
                trace.SetLogPath(basePath + "//" + namespaceName);
                trace.G_LogEnable = g_LogEnable;
                // 建立连接
                //GetInternetCard();
                //GetIP();
                //Connect(Ip);
                // 3. 读取文件并遍历写入
                using (FileStream fileStream = new FileStream(CsvPath, FileMode.Open, FileAccess.Read))
                using (StreamReader streamReader = new StreamReader(fileStream, Encoding.Default))
                {
                    string strline = null;
                    string[] arrayLine = null;
                    int line_No = 1;
                    int Mode = 0;
                    int sum_step = 0;
                    while ((strline = streamReader.ReadLine()) != null)
                    {
                        strline = strline.Trim();
                        arrayLine = strline.Split(',');

                        #region csv文件第一行
                        if (line_No == 1)
                        {
                            if (arrayLine[1] == "外触发")
                            {
                                Mode = 1;
                            }
                            else if (arrayLine[1] == "内触发")
                            {
                                Mode = 2;
                            }
                            else
                            {
                                Mode = 3;
                            }
                            // 设置触发模式
                            if (!(SetTriggerMode(Rs232_or_Ethernet, Mode, controllerHandle) == SUCCESS))
                            {
                                if (arrayLine[1] == "外触发")
                                {
                                    ErrorText = $"设置外触发模式失败";
                                    ErrorCode = 101;
                                    Occurred = true;
                                    trace.Info(ErrorText);
                                }
                                else if (arrayLine[1] == "内触发")
                                {
                                    ErrorText = $"设置内触发模式失败";
                                    ErrorCode = 102;
                                    Occurred = true;
                                    trace.Info(ErrorText);
                                }
                                else
                                {
                                    ErrorText = $"设置常亮模式失败";
                                    ErrorCode = 103;
                                    Occurred = true;
                                    trace.Info(ErrorText);
                                }
                            }

                            if (arrayLine[3] == "IO触发")
                            {
                                Mode = 1;
                            }
                            else
                            {
                                Mode = 2;
                            }
                            // 设置外部触发器模式
                            if (!(SetExternalTriggerMode(Rs232_or_Ethernet, Mode, controllerHandle) == SUCCESS))
                            {
                                if (arrayLine[3] == "IO触发")
                                {
                                    ErrorText = $"设置IO触发模式失败";
                                    ErrorCode = 104;
                                    Occurred = true;
                                    trace.Info(ErrorText);
                                }
                                else
                                {
                                    ErrorText = $"设置PLC触发模式失败";
                                    ErrorCode = 105;
                                    Occurred = true;
                                    trace.Info(ErrorText);
                                }
                            }
                            int numericUpDown = 0;
                            if (arrayLine[6] == "滤波时间")
                            {
                                if (Convert.ToInt32(arrayLine[7]) <= 999)
                                {
                                    numericUpDown = Convert.ToInt32(arrayLine[7]);
                                }
                            }
                            // 设置滤波时间
                            if (!(SetFilteringTime(Rs232_or_Ethernet, numericUpDown, controllerHandle) == SUCCESS))
                            {
                                ErrorText = $"设置滤波时间失败";
                                ErrorCode = 106;
                                Occurred = true;
                                trace.Info(ErrorText);
                            }
                        }

                        #endregion

                        #region csv文件第四行
                        if (line_No == 4)
                        {
                            int[] Lightdata1 = new int[12];
                            for (int i = 0; i < 12; i++)
                            {
                                Lightdata1[i] = Convert.ToInt32(arrayLine[1 + i]);
                            }
                            if (SetConstantLightModeData(Rs232_or_Ethernet, Lightdata1, controllerHandle) == SUCCESS)
                            {
                                string temp = "";
                                for (int i = 0; i < 12; i++)
                                {
                                    temp = temp + "CH" + (i + 1) + "亮度：（";
                                    temp = temp + Lightdata1[i] + "）. ";
                                }
                            }
                            else
                            {
                                ErrorText = $"设置常亮失败";
                                ErrorCode = 107;
                                Occurred = true;
                                trace.Info(ErrorText);
                            }
                        }
                        #endregion

                        #region csv文件第7~10行
                        if (line_No >= 7)
                        {
                            int[] Lightdata = new int[24];
                            int[] PulseWidth = new int[24];

                            for (int i = 0; i < 12; i++)
                            {
                                PulseWidth[i] = Convert.ToInt32(arrayLine[1 + 2 * i]);
                            }

                            for (int i = 0; i < 12; i++)
                            {
                                Lightdata[i] = Convert.ToInt32(arrayLine[2 + 2 * i]);
                            }
                            //var aaa = SetProgrammableData(Rs232_or_Ethernet, line_No - 7 + 1, Lightdata, PulseWidth, controllerHandle);
                            if (SetProgrammableData(Rs232_or_Ethernet, line_No - 7 + 1, Lightdata, PulseWidth, controllerHandle) == SUCCESS)
                            {
                                string temp = "";
                                for (int i = 0; i < 12; i++)
                                {
                                    temp = temp + "CH" + (i + 1) + "脉宽、亮度：（";
                                    temp = temp + PulseWidth[i] + ",";
                                    temp = temp + Lightdata[i] + "）.";
                                }
                            }
                            else
                            {
                                ErrorText = $"设置流程{(line_No - 7 + 1)}可编程数据失败";
                                ErrorCode = 108;
                                Occurred = true;
                                trace.Info(ErrorText);
                            }
                            sum_step++;
                        }

                        line_No++;
                        #endregion
                    }
                    if (!(SetTotalSteps(Rs232_or_Ethernet, sum_step, controllerHandle) == SUCCESS))
                    {
                        ErrorText = $"设置步骤可编程参数失败";
                        ErrorCode = 109;
                        Occurred = true;
                        trace.Info(ErrorText);
                    }

                    if (!(SaveData(Rs232_or_Ethernet, controllerHandle) == SUCCESS))
                    {
                        ErrorText = $"保存数据失败";
                        ErrorCode = 110;
                        Occurred = true;
                        trace.Info(ErrorText);
                    }
                }


                result = 1;
            }
            catch (Exception e)
            {
                ErrorCode = -1;
                ErrorText = $"light controller dll set info error:{e}";
                Occurred = true;
                trace.Info(ErrorText);
            }
            // 因为还要触发光源，所以不能静默保存完就执行关闭连接操作
            //connect();
            return true;
        }

        static List<string> list1 = new List<string>();
        static List<string> list2 = new List<string>();
        static string choose1 = "";
        static string choose2 = "";

        /// <summary>
        /// 获取光源控制器网卡信息
        /// </summary>
        static void GetInternetCard()
        {
            list1.Clear();
            list2.Clear();
            int AdapterNum = 0;

            int workStationCount = 30;
            int size = Marshal.SizeOf(typeof(Adapter_prm));
            IntPtr mAdapterPrmPtr = Marshal.AllocHGlobal(size * workStationCount);
            Adapter_prm[] mAdapter_prm = new Adapter_prm[workStationCount];
            if (GetAdapter(ref AdapterNum, mAdapterPrmPtr) == SUCCESS)
            {
                for (int inkIndex = 0; inkIndex < workStationCount; inkIndex++)
                {
                    IntPtr ptr = (IntPtr)(mAdapterPrmPtr + inkIndex * size);
                    mAdapter_prm[inkIndex] = (Adapter_prm)Marshal.PtrToStructure(ptr, typeof(Adapter_prm));
                }

                string[] SnBuffer = new string[AdapterNum];
                for (int i = 0; i < AdapterNum; i++)
                {
                    SnBuffer[i] = new string(mAdapter_prm[i].cSn);
                    list1.Add(SnBuffer[i]);
                }
                string[] IpBuffer = new string[AdapterNum];
                for (int i = 0; i < AdapterNum; i++)
                {
                    IpBuffer[i] = new string(mAdapter_prm[i].cIp);
                    list2.Add(IpBuffer[i]);
                }
                // 设置网卡IP
                choose2 = IpBuffer[1];
            }
            else
            {
                throw new Exception("获取网卡信息失败");

            }
        }

        static List<string> list3 = new List<string>();
        static string choose3 = "";
        static string ipAddress = "";
        static string sm = "";
        static string gw = "";
        static string sn = "";
        static int workStationCount = 50;
        static Host_prm[] mHost_prm = new Host_prm[workStationCount];
        static bool? dhcp = null;
        static void GetIP()
        {
            int controllerCnt = 0;
            list3.Clear();
            string AdapterIP = choose2;
            int size = Marshal.SizeOf(typeof(Host_prm));
            IntPtr mHostPrmPtr = Marshal.AllocHGlobal(size * workStationCount);
            //获取设备
            if (GetHost(ref controllerCnt, mHostPrmPtr, AdapterIP) == SUCCESS)
            {
                for (int inkIndex = 0; inkIndex < workStationCount; inkIndex++)
                {
                    IntPtr ptr = (IntPtr)(mHostPrmPtr + inkIndex * size);
                    mHost_prm[inkIndex] = (Host_prm)Marshal.PtrToStructure(ptr, typeof(Host_prm));
                }

                if (controllerCnt > 0)
                {
                    string[] SnBuffer = new string[controllerCnt];
                    for (int i = 0; i < controllerCnt; i++)
                    {
                        SnBuffer[i] = new string(mHost_prm[i].cSn);
                        list3.Add(SnBuffer[i]);
                        choose3 = SnBuffer[0];
                        sn = choose3;
                    }

                    string[] IpBuffer = new string[controllerCnt];
                    for (int i = 0; i < controllerCnt; i++)
                    {
                        IpBuffer[i] = new string(mHost_prm[i].cIp);
                        ipAddress = IpBuffer[0];
                    }
                }
                else
                {
                    throw new Exception($"该网卡下控制器数量为：{controllerCnt}");
                }
            }
            else
            {
                throw new Exception("获取控制器信息失败");
            }

            int size1 = Marshal.SizeOf(typeof(Controller_prm));
            IntPtr mConPrmPtr = Marshal.AllocHGlobal(size1);
            Controller_prm mConPrm;
            if (GetConfigure(mHost_prm[0].cMac, mConPrmPtr, AdapterIP) == SUCCESS) //获取参数
            {
                IntPtr ptr = (IntPtr)(mConPrmPtr);
                mConPrm = (Controller_prm)Marshal.PtrToStructure(ptr, typeof(Controller_prm));

                string SmBuffer;
                SmBuffer = new string(mConPrm.cSm);
                sm = SmBuffer;

                string GwBuffer;
                GwBuffer = new string(mConPrm.cGw);
                gw = GwBuffer;

                string IpBuffer;
                IpBuffer = new string(mConPrm.cIp);
                // 设置IP地址
                ipAddress = IpBuffer;

                if (mConPrm.DHCP == 0)
                {
                    dhcp = false;
                }
                else
                {
                    dhcp = true;
                }
            }
        }

        static string buttonText = "IP连接";

        /// <summary>
        /// 连接操作
        /// </summary>
        public static void Connect(string Ip, int ControllerHandle, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "succese";
            Occurred = false;
            if (buttonText == "IP连接")
            {
                //string IP = ipAddress.ToString();
                int RT1 = ConnectIP(Ip, 1, ref ControllerHandle);
                if (RT1 == SUCCESS)
                {
                    Rs232_or_Ethernet = EthernetMode;

                    buttonText = "断开连接";
                    connect_state = 1;
                }
                else
                {
                    ErrorCode = 0;
                    ErrorText = "IP连接控制器失败";
                    Occurred = true;
                    trace.Info(ErrorText);
                }
            }
            else if (buttonText == "断开连接")
            {
                int RT1 = DestroyIpConnection(ControllerHandle); //断开IP连接
                if (RT1 == SUCCESS)
                {
                    buttonText = "IP连接";
                    connect_state = 0;
                }
                else
                {
                    ErrorCode = 0;
                    ErrorText = "IP断开控制器失败";
                    Occurred = true;
                    trace.Info(ErrorText);
                }
            }
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        public static bool DisConnect(int ControllHandle,  out int ErrorCode, out string ErrorText, out bool Occurred)
        {   
            ErrorCode = 0;
            ErrorText = "succese";
            Occurred = false;
            if (buttonText == "断开连接")
            {
                if (SetInternalTriggerMode(Rs232_or_Ethernet, 3, 0, ControllHandle) != SUCCESS)
                {
                    
                    ErrorCode = -1;
                    ErrorText = "停止流程失败";
                    Occurred = true;
                    trace.Info(ErrorText);
                }
                int RT1 = DestroyIpConnection(ControllHandle); //断开IP连接
                if (RT1 == SUCCESS)
                {
                    buttonText = "IP连接";
                    connect_state = 0;

                }
                else
                {
                    ErrorCode = -2;
                    ErrorText = "断开IP失败";
                    Occurred = true;
                    trace.Info(ErrorText);
                    throw new Exception("IP断开控制器失败");
                }
            }
            return true;
        }

        
        /// <summary>
        /// 设置触发模式
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public static bool TriggerMode(int Mode, int ControllHandle , out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            if (!(SetTriggerMode(Rs232_or_Ethernet, Mode, ControllHandle) == SUCCESS))
            {
                if (Mode==1)
                {
                    ErrorText = $"设置外触发模式失败";
                    ErrorCode = 101;
                    Occurred = true;
                    trace.Info(ErrorText);
                }
                else if (Mode == 2)
                {
                    ErrorText = $"设置内触发模式失败";
                    ErrorCode = 102;
                    Occurred = true;
                    trace.Info(ErrorText);
                }
                else
                {
                    ErrorText = $"设置常亮模式失败";
                    ErrorCode = 103;
                    Occurred = true;
                    trace.Info(ErrorText);
                }
            }
            return true;
        }

        /// <summary>
        /// 触发光源
        /// </summary>
        /// <param name="TriggerMode">触发模型</param>
        /// <param name="data">触发流程</param>
        /// <param name="ErrorCode">the code for error</param>
        /// <param name="ErrorText">the message for error</param>
        /// <returns></returns>
        public static bool TriggerLight(int TriggerMode, int ControllHandle, int data, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int i = SetInternalTriggerMode(Rs232_or_Ethernet, TriggerMode, data, ControllHandle);
            if (i != SUCCESS)
            {
                Occurred = true;
                ErrorCode = i;
                ErrorText = "触发失败";
                trace.Info(ErrorText);
            }
            return true;
        }

        /// <summary>
        /// 打开常亮光源
        /// </summary>
        /// <param name="Lightdata1"></param>
        /// <param name="LightTime"></param>
        /// <param name="ControllHandle"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public static bool OpenLight(int[] Lightdata1,int LightTime, int ControllHandle, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            if (connect_state == 1)
            {
                return true;
            }
            if (SetConstantLightModeData(Rs232_or_Ethernet, Lightdata1, ControllHandle) == SUCCESS)
            {

                //return true;
            }
            else
            {
                Occurred = true;
                ErrorCode = -1019;
                ErrorText = "设置常亮参数失败";
                trace.Info(ErrorText);//textBoxMessageShow.Text = $"设置常亮参数失败";
                return false;
            }
            if (SetConstantLightTime(Rs232_or_Ethernet, 2, LightTime, ControllHandle) == SUCCESS)
            {
                //textBoxMessageShow.Text = $"成功开始点亮";
                //log_write($"Set参数-----常亮模式 开始点亮");
                return true;
            }
            else
            {

                Occurred = true;
                ErrorCode = -1020;
                ErrorText = "设置开始点亮失败";
                trace.Info(ErrorText);//textBoxMessageShow.Text = $"设置开始点亮失败";
                return false;
            }
        }

        /// <summary>
        /// 关闭常亮光源
        /// </summary>
        /// <param name="LightTime"></param>
        /// <param name="ControllHandle"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public static bool CloseLight(int LightTime, int ControllHandle, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            if (connect_state == 1)
            {
                return true;
            }
            
            if (SetConstantLightTime(Rs232_or_Ethernet, 3, LightTime, ControllHandle) == SUCCESS)
            {
                //textBoxMessageShow.Text = $"成功熄灭";
                //log_write($"Set参数-----常亮模式 熄灭");
                return true;
            }
            else
            {
                Occurred = true;
                ErrorCode = -1021;
                ErrorText = "熄灭失败";
                trace.Info(ErrorText);
                //textBoxMessageShow.Text = $"熄灭失败";
                return true;
            }
        }
    }
    internal class SoftTriggerControl_RS
    {
        private static LogTrace trace = new LogTrace();
        /// <summary>
        /// 链接光源
        /// </summary>
        public static bool ConnectNet(string Ip, int iPort, out uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            DataSocket=RseeController_ConnectNet(Ip, iPort);
            if (DataSocket == 0)
            {
                ErrorCode = -200;
                ErrorText = "光源网口连接失败";
                Occurred = true;
                trace.Info(ErrorText);
                return false;

            }
            trace.Info(ErrorText);
            return true;
        }

        /// <summary>
        /// 断开链接
        /// </summary>
        public static bool CloseNet(uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            if (!RseeController_CloseNet(DataSocket))
            {
                ErrorCode = -201;
                ErrorText = "网口断开失败";
                Occurred = true;
                trace.Info(ErrorText);
                return false; ;
            }
            
            return true;
        
        }

        /// <summary>
        /// 脱机/在线 模式设置  OnOFF– false：脱机，true：在线
        /// </summary>
        public static bool OfflineOrOnline(uint DataSocket, bool OnOFF, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int state = RseeController_P_S2_24V_10TCL_TriEn(DataSocket, OnOFF);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OfflineOrOnline" + ErrorText;
                Occurred = true;
                trace.Info(ErrorText);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 工作模式（常亮/IO外触发/PLC触发/内触发）mode – 工作模式，0：常亮模式  1：IO触发模式  2：PLC触发模式  3：内触发模式
        /// </summary>
        public static bool TriMode(uint DataSocket, int mode, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int state = RseeController_P_S2_24V_10TCL_TriMode(DataSocket, mode);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "TriMode" + ErrorText;
                Occurred = true;
                trace.Info(ErrorText);
                return false;
            }
            return true;

        }
        /// <summary>
        /// 打开常亮光源，LightDatas为10位通道亮度数组，范围0~255。LightTime为常亮时间
        /// </summary>
        public static bool OpenLight(uint DataSocket, int[] LightDatas, int LightTime, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int state1 = RseeController_P_S2_24V_10TCL_SetRange(DataSocket, LightDatas);
            if (state1 != 0)
            {
                StateOut(state1, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OpenLight_SetRange" + ErrorText;
                Occurred = true;
                trace.Info(ErrorText);
                return false;
            }
            int state2 = RseeController_P_S2_24V_10TCL_LimitTime(DataSocket, LightTime);
            if (state2 != 0)
            {
                StateOut(state2, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OpenLight_LimitTime" + ErrorText;
                Occurred = true;
                trace.Info(ErrorText);
                return false;
            }
            int state3 = RseeController_P_S2_24V_10TCL_Onoff(DataSocket,true);
            if (state3 != 0)
            {
                StateOut(state3, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OpenLight_DataSocket" + ErrorText;
                Occurred = true;
                trace.Info(ErrorText);
                return false;
            }
            return true;

        }

        /// <summary>
        /// 关闭常亮光源
        /// </summary>
        public static bool CloseLight(uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            
            int state = RseeController_P_S2_24V_10TCL_Onoff(DataSocket, false);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "CloseLight" + ErrorText;
                Occurred = true;
                trace.Info(ErrorText);
                return false;
            }
            return true;    
        }

        /// <summary>
        /// 复位重启
        /// </summary>
        public static bool ErrReset(uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            
            int state = RseeController_P_S2_24V_10TCL_ErrReset(DataSocket);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "ErrReset" + ErrorText;
                Occurred = true;
                trace.Info(ErrorText);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 错误信息输出
        /// </summary>
        /// <param name="ReturnValue"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public static bool StateOut(int ReturnValue, out int ErrorCode, out string ErrorText, out bool Occurred)
        {

            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            if (ReturnValue == -1)
            {
                ErrorCode = -1;
                ErrorText = "设置/读取失败";
                Occurred = false;

            }
            else if (ReturnValue == -2)
            {
                ErrorCode = -2;
                ErrorText = "指令格式出错";
                Occurred = false;
            }
            else if (ReturnValue == -3)
            {
                ErrorCode = -3;
                ErrorText = "校验出错";
                Occurred = false;
            }
            else if (ReturnValue == -4)
            {
                ErrorCode = -4;
                ErrorText = "指令代码编号错误";
                Occurred = false;
            }
            else if (ReturnValue == -5)
            {
                ErrorCode = -5;
                ErrorText = "指令参数不在范围内";
                Occurred = false;
            }
            else if (ReturnValue == -6)
            {
                ErrorCode = -6;
                ErrorText = "指令使用场合不正确";
                Occurred = false;
            }
            else if (ReturnValue == -7)
            {
                ErrorCode = -7;
                ErrorText = "存储区无数据 ";
                Occurred = false;
            }
            else if (ReturnValue == -8)
            {
                ErrorCode = -8;
                ErrorText = "设置步骤超出流程长度";
                Occurred = false;
            }
            return true;

        }
    }
}
